﻿using Contacts.UseCases.PluginInterfaces;
using Contact = Contacts.CoreBusiness.Contact;

namespace Contacts.Plugins.DataStore.InMemory
{
    public class ContactInMemoryRepository : IContactRepository
    {
        public static List<Contact> _contactList;

        public ContactInMemoryRepository()
        {
            _contactList = new List<Contact>()
            {
               new Contact(){ ContactId=1, Name="11111", Email="11@qq.com"},
               new Contact(){ ContactId=2, Name="222222", Email="22@qq.com"},
               new Contact(){ ContactId=3, Name="333333", Email="33@qq.com"},
            };
        }

        public Task AddContactAsync(Contact contact)
        {
            int MaxContactId = _contactList.Max(a => a.ContactId);
            int ContactId = MaxContactId + 1;
            _contactList.Add(contact);
            return Task.CompletedTask;
        }

        public Task DeleteContactAsync(int contactId)
        {
            var contact = _contactList.FirstOrDefault(a => a.ContactId == contactId);
            if (contact != null)
            {
                _contactList.Remove(contact);
            }
            return Task.CompletedTask;
        }

        public Task<Contact> GetContactByIdAsync(int contactId)
        {
            var contact = _contactList.FirstOrDefault(a => a.ContactId == contactId);
            if (contact != null)
            {
                return Task.FromResult(new Contact
                {
                    ContactId = contact.ContactId,
                    Name = contact.Name,
                    Email = contact.Email,
                    Phone = contact.Phone,
                    Adress = contact.Adress,
                });
            }
            return null;
        }

        public Task<List<Contact>> GetContactsAsync(string filterText)
        {
            if (string.IsNullOrEmpty(filterText))
                return Task.FromResult(_contactList);

            var contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Name) && a.Name.StartsWith(filterText, StringComparison.OrdinalIgnoreCase)).ToList();

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Email) && a.Email.StartsWith(filterText, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return Task.FromResult(contact);

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Phone) && a.Phone.StartsWith(filterText, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return Task.FromResult(contact);

            if (contact == null || contact.Count <= 0)
                contact = _contactList.Where(a => !string.IsNullOrEmpty(a.Adress) && a.Adress.StartsWith(filterText, StringComparison.OrdinalIgnoreCase)).ToList();
            else
                return Task.FromResult(contact);

            return Task.FromResult(contact);
        }

        public Task UpdateContactAsync(int contactId, Contact contact)
        {
            if (contactId != contact.ContactId) return Task.CompletedTask;

            var contactUpdate = _contactList.FirstOrDefault(a => a.ContactId == contactId);
            if (contactUpdate != null)
            {
                contactUpdate.Name = contact.Name;
                contactUpdate.Email = contact.Email;
                contactUpdate.Phone = contact.Phone;
                contactUpdate.Adress = contact.Adress;
            }
            return Task.CompletedTask;
        }
    }
}
